<?php
namespace plugins\sms\controller;
use cmf\controller\PluginAdminBaseController;
use plugins\site_config\model\SiteInfoModel;
use plugins\sms\lib\Common as LCOM;

//短信配置首页
class AdminIndexController extends PluginAdminBaseController
{
    //短信信息
    public function index()
    {
        $siteInfo = LCOM::getConfig();
        
        $list = LCOM::getAllList();
        
        $enabled = LCOM::getConfig();
        
        $smsConfigList = $this->getSmsConfigList( $list );

        
        //获取配置页
        
        $this->assign( 'enabled' , $enabled  );
        $this->assign( 'list' , $list );
        $this->assign( 'smsConfigList' , $smsConfigList );
        return $this->fetch();
        
    }
    
    //获取短信配置列表
    private function getSmsConfigList( $smsConfigList )
    {
        $list = [];
        foreach( $smsConfigList as $key => $value ){
            $class = '\\plugins\\sms\\controller\\'.cmf_parse_name( $value [ 'type' ] , 1 ).'Controller';
            if( class_exists( $class ) ){
                $obj = new $class();
                if( method_exists( $obj , 'config' ) ){
                    $list [ $value [ 'type' ] ] [ 'page' ] = $obj->config( '<include file="'.strtolower( $value [ 'type' ] ).'/config" />' );
                    $list [ $value [ 'type' ] ] [ 'name' ] = $value[ 'name' ];
                }
            }
        }
        return $list;
    }
    
    
    //保存配置信息
    public function switchon()
    {
        $data = $this->request->param()[ 'config' ];
        
        if( LCOM::setConfig( $data ) ){
            return api_json_echo( true , '保存成功' );
        }
        return api_json_echo( false , '保存失败' );
    }
    
    

    
    
    
}